#!/bin/env ruby
# encoding: utf-8
class UserTracesController < ApplicationController
  
  def search_project
    type = "search_project"
    keyword = params[:keyword]
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["keyword" => keyword]
    writeDatabase(logger_search,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def search_knowledge
    type = "search_knowledge"
    keyword = params[:keyword]
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["keyword" => keyword]
    writeDatabase(logger_search,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_project_after_search
    type = "click_project_after_search"
    page = params[:page]
    rank = params[:rank]
    id = params[:id]
    keyword = params[:keyword]
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["keyword" => keyword, "page" => page, "rank" => rank, "osp_id" => id]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_project_in_project_list
    type = "click_project_in_project_list"
    page = params[:page]
    rank = params[:rank]
    id = params[:id]
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["page" => page, "rank" => rank, "osp_id" => id]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_project_in_cloud
    type = "click_project_in_cloud"
    osp_id = params[:osp_id]
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id" => osp_id]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_project_source_in_detail
    type = "click_project_source_in_detail"
    source = params[:source]
    url = params[:url]
    rank = params[:rank]
    osp_id = params[:osp_id]
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id" => osp_id, "source" => source, "url" => url, "rank" => rank]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_similar_project
    type = "click_similar_project"
    osp_id_former = params[:osp_id_former]
    osp_id_similar = params[:osp_id_similar]
    rank = params[:rank]
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id_former" => osp_id_former,"rank" => rank, "osp_id_similar" => osp_id_similar]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_similar_project_down
    type = "click_similar_project_down"
    osp_id_former = params[:osp_id_former]
    osp_id_similar = params[:osp_id_similar]
    rank = params[:rank]
    
    # 修改数据库中的信息 simility_recommend_up_downs表中up_down_value字段-1
    # 判断数据库中是否存在相应记录
    similityRecommendUpDown = SimilityRecommendUpDown.where("osp_id=? and relative_osp_id=?", osp_id_former, osp_id_similar).limit(1)
    if similityRecommendUpDown[0] != nil
      up_down_value = similityRecommendUpDown[0].up_down_value - 1
      SimilityRecommendUpDown.update(similityRecommendUpDown[0].id.to_i, :up_down_value => up_down_value)
    else
      # 表示数据库中没有记录 需要insert
      # SimilityRecommendUpDown.insert("osp_id, ")
      item = SimilityRecommendUpDown.new(:osp_id => osp_id_former, :relative_osp_id => osp_id_similar, :up_down_value => -1)
      item.save
    end
    
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id_former" => osp_id_former,"rank" => rank, "osp_id_similar" => osp_id_similar]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_similar_project_up
    type = "click_similar_project_up"
    osp_id_former = params[:osp_id_former]
    osp_id_similar = params[:osp_id_similar]
    rank = params[:rank]
    
    # 修改数据库中的信息 simility_recommend_up_downs表中up_down_value字段+1
    # 判断数据库中是否存在相应记录
    similityRecommendUpDown = SimilityRecommendUpDown.where("osp_id=? and relative_osp_id=?", osp_id_former, osp_id_similar).limit(1)
    if similityRecommendUpDown[0] != nil
      up_down_value = similityRecommendUpDown[0].up_down_value + 1
      SimilityRecommendUpDown.update(similityRecommendUpDown[0].id.to_i, :up_down_value => up_down_value, :last_up_time => DateTime.now)
    else
      # 表示数据库中没有记录 需要insert
      # SimilityRecommendUpDown.insert("osp_id, ")
      item = SimilityRecommendUpDown.new(:osp_id => osp_id_former, :relative_osp_id => osp_id_similar, :up_down_value => 1, :last_up_time => DateTime.now)
      item.save
    end
    
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id_former" => osp_id_former,"rank" => rank, "osp_id_similar" => osp_id_similar]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_related_project
    type = "click_related_project"
    osp_id_former = params[:osp_id_former]
    osp_id_related = params[:osp_id_related]
    rank = params[:rank]
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id_former" => osp_id_former,"rank" => rank, "osp_id_related" => osp_id_related]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_project_list
    type = "click_project_list"
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash.new
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_more_tags
    type = "click_more_tags"
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id" => params[:osp_id]]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_tag
    type = "click_tag" #还可能是帖子的标签
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id" => params[:osp_id], "rank" => params[:rank], "tag_name" => params[:tag_name]]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_knowledge_after_search
    type = "click_knowledge_after_search"
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["keyword" => params[:keyword], "sort" => params[:sort], "page" => params[:page], "rank" => params[:rank], "knowledge_id" => params[:id]]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_knowledge_in_project_detail
    type = "click_knowledge_in_project_detail"
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id" => params[:osp_id], "memo_type" => params[:memo_type], "page" => params[:page], "rank" => params[:rank], "knowledge_id" => params[:memo_id], "href" => params[:href]]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_more_knowledge_in_project_detail
    type = "click_more_knowledge_in_project_detail"
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id" => params[:osp_id], "memo_type" => params[:type]]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_knowledge_in_project_detail_after_click_more
    type = "click_knowledge_in_project_detail_after_click_more"
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["osp_id" => params[:osp_id], "memo_type" => params[:memo_type], "page" => params[:page], "rank" => params[:rank], "knowledge_id" => params[:memo_id], "sort_by" => params[:sort]]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_project_after_tag_search
    type = "click_project_after_tag_search"
    logger_search,logger_click,session_id,ip,host,time = generateHead
    keyword = params[:keyword]
    if keyword.length == 0
      keyword = 'nil'
    end
    parameter = Hash["keyword" => keyword, "page" => params[:page], "rank" => params[:rank], "osp_id" => params[:osp_id]]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_project_img_after_search
    type = "click_project_img_after_search"
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["keyword" => params[:keyword], "page" => params[:page], "rank" => params[:rank], "osp_id" => params[:osp_id]]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  def click_project_img_in_project_list
    type = "click_project_img_in_project_list"
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["page" => params[:page], "rank" => params[:rank], "osp_id" => params[:osp_id]]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
        format.json {render :json => {}}
    end
  end
  
  # 定义函数 将trustie_user_id存储到session中
  def add_trustie_user_id
    # 获取参数
    trustie_user_id = params[:trustie_user_id]
    if trustie_user_id != nil
      # 判断长度是否合乎规范
      if trustie_user_id.length <= 11
        # 表示可能是int型数据
        # 看一下id是否符合规范 是否是真正跳转过来的
        pattern = /\D+/ # 表示字符串中有非数字的字符
        index = (trustie_user_id =~ pattern)
        if index == nil
          # 表示合法的trustie_user_id
          session[:trustie_user_id] = trustie_user_id.to_i
        else
          session[:trustie_user_id] = nil # 表示trustie_user_id中有除了数字之外的字符
        end
      else
        session[:trustie_user_id] = nil # 表示长度不合法
      end
    else
        session[:trustie_user_id] = nil # 表示为空
    end
    respond_to do |format|
      format.json {render :json => {}}
    end
  end
  
  
  # 定义函数捕获用户点击link的操作
  def click_link
    # url = request.original_url
    # url = URI.unescape(url) # 获取url跳转的url地址lick_link" # 表示点击了a链接
    href = params[:href]# 获取href参数
    logger_search,logger_click,session_id,ip,host,time = generateHead
    parameter = Hash["url" => href]
    writeDatabase(logger_click,type,session_id,ip,host,time,parameter)
    respond_to do |format|
      format.json {render :json => {}}
    end
  end
  
  private
  def generateHead
    #创建一个新的日志
    # filename = File.join(Rails.root, 'log', 'user_trace_search.log')
    # logger_search = Logger.new(filename)
#     
    # #创建用户click的日志
    # filename = File.join(Rails.root, 'log', 'user_trace_click.log')
    # logger_click = Logger.new(filename)
    
    logger_search = nil
    logger_click = nil
    
    session_id = params[:session_id]
    if session_id == nil
      session[:session_id]
      session_id = request.session_options[:id]
    end
    # if session_id == nil
      # session_id = params[:session_id]
    # end
    
    # ip = request.remote_ip 
    ip = Socket.ip_address_list.detect(&:ipv4_private?).try(:ip_address)
    # host = request.host
    # require 'socket'
    host = Socket.gethostname
    #获取时间
    time = Time.new.localtime
    # year = time.year.to_s
    # month = time.month.to_s
    # day = time.day.to_s
    # hour = time.hour.to_s
    # minute = time.min.to_s
    # second = time.sec.to_s
    # time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
    
    return logger_search,logger_click,session_id,ip,host,time
  end
  
  def writeLog(logger,type,session_id,ip,host,time,parameter)
    if type != nil && session_id != nil && ip != nil && host != nil && session_id != 'null' && session_id != ""
      #表示是需要输入的类型
      str = "type:" + type + "\t" + "session_id:" + session_id + "\t" + "ip:" + ip + "\t" + "host:" + host + "\t" + "time:" + time + "\t"
      keys_in_parameter = parameter.keys
      keys_in_parameter.each do |k|
        value = parameter[k]
        str = str + "[" + k + "=>" + value.to_s + "]"
      end
      logger.info(str.strip)
    end
  end
  
  # 张迅晖 将用户行为监控存储到数据库中
  def writeDatabase(logger,type,session_id,ip,host,time,parameter) # logger为了和writeLog保持一致的参数 在该函数中没有实际作用
    # puts "~~~~session_id"
    # if type != nil && session_id != nil && ip != nil && host != nil && session_id != 'null' && session_id != ""
      # 读取session中trustie_user_id
      trustie_user_id = session[:trustie_user_id]
      # 判断trustie_user_id是否为nil 如果为nil表示不是trustie跳转过来的用户 如果不是nil表示是trustie链接跳转过来的用户
      params = "" # 用于存储参数
      keys_in_parameter = parameter.keys
      keys_in_parameter.each do |k|
        value = parameter[k]
        params = params + "[" + k + "=>" + value.to_s + "]"
      end
      params = params.strip # 获取到了存储的参数
      
      # 将结果存储到数据库中
      userAction = UserAction.new
      userAction.trustie_user_id = trustie_user_id
      userAction.session_id = session_id
      userAction.type = type
      userAction.ip = ip
      userAction.host = host
      userAction.time = time
      userAction.params = params
      userAction.save
    # end
  end
  
  def getCurrentTime
    time = Time.new.localtime
    year = time.year.to_s
    month = time.month.to_s
    day = time.day.to_s
    hour = time.hour.to_s
    minute = time.min.to_s
    second = time.sec.to_s
    time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
    return time
  end
end
